package Entwickler;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Window extends JFrame implements MouseListener, KeyListener{
	
	BufferedImage bufferImage = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
	Graphics2D bufferedGraphics2d;
	ArrayList<Polygon> polygons = new ArrayList<Polygon>();
	Polygon vectors;
	boolean shiftHold = false;
	
	public Window(){
		vectors = BasicMeshCreator.createVectors();
		setLayout(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(800, 600);
		setLocation(300, 50);
		setResizable(false);
		setTitle("3D-Entwickler");
		setVisible(true);
		polygons.add(BasicMeshCreator.createCube());
		addKeyListener(this);
		addMouseListener(this);
		update();
	}
	
	void update() {
		paint(this.getGraphics());
	}
	
	public void paint(Graphics g) {
		bufferedGraphics2d = (Graphics2D)bufferImage.getGraphics();
		bufferedGraphics2d.setColor(Color.white);
		bufferedGraphics2d.fillRect(0, 0, 800, 600);

		for (int i = 0; i < polygons.size(); i++) {
			polygons.get(i).paint(bufferedGraphics2d);
		}
		vectors.xPos = -7;
		vectors.yPos = 4;
		vectors.paint(bufferedGraphics2d);
		vectors.xPos = 0;
		vectors.yPos = 0;
		
		g.drawImage(bufferImage, 0, 0, null);
	}
	
	public void rotateView(double x, double y, double z){
		vectors.rotate(x, y, z);
		for (int i = 0; i < polygons.size(); i++) {
			polygons.get(i).rotate(x, y, z);
		}
	}
	
	public void zoomView(double zoom){
		for (int i = 0; i < polygons.size(); i++) {
			polygons.get(i).scale(zoom, zoom, zoom);
		}
	}
	
	public void moveView(double x, double y){
		for (int i = 0; i < polygons.size(); i++) {
			polygons.get(i).xPos += x;
			polygons.get(i).yPos += y;
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();
	    switch(keyCode) { 
	        case KeyEvent.VK_UP:
	        	if(shiftHold){
	        		moveView(0, -0.2);
	        	}else{
	        		rotateView(Math.PI/50, 0, 0);
	        	}
	            break;
	        case KeyEvent.VK_DOWN:
	        	if(shiftHold){
	        		moveView(0, 0.2);
	        	}else{
	        		rotateView(-Math.PI/50, 0, 0);
	        	}
	            break;
	        case KeyEvent.VK_LEFT:
	        	if(shiftHold){
	        		moveView(0.2, 0);
	        	}else{
	        		rotateView(0, -Math.PI/50, 0);
	        	}
	            break;
	        case KeyEvent.VK_RIGHT:
	        	if(shiftHold){
	        		moveView(-0.2, 0);
	        	}else{
	        		rotateView(0, Math.PI/50, 0);
	        	}
	            break;
	        case KeyEvent.VK_A:
	        	rotateView(0, 0, -Math.PI/50);
	            break;
	        case KeyEvent.VK_S:
	        	rotateView(0, 0, Math.PI/50);
	            break;
	        case KeyEvent.VK_Q:
	        	zoomView(1.05);
	            break;
	        case KeyEvent.VK_W:
	        	zoomView(0.95);
	            break;
	        case KeyEvent.VK_C:
	        	polygons.add(BasicMeshCreator.createCube());
	            break;
	        case KeyEvent.VK_ESCAPE:
	        	polygons.clear();
	            break;
	        case KeyEvent.VK_O:
	        	polygons.add(OBJLoader.Load(JOptionPane.showInputDialog("Dateipfad")));
	            break;
	        case KeyEvent.VK_SHIFT:
	        	shiftHold = true;
	            break;
	     }
	    update();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		int keyCode = e.getKeyCode();
	    switch(keyCode) { 
	    	case KeyEvent.VK_SHIFT:
	    		shiftHold = false;
	    		break;
	    }
	}

	@Override
	public void keyTyped(KeyEvent e) {
		
	}
	
}

